package cn.yunhe.controller;

import cn.yunhe.entity.Member;
import cn.yunhe.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @RequestMapping("/findAll")
    public String findAll(Model model,
                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        PageHelper.startPage(page,size);
        List<Member> memberList = memberService.findAll();
        PageInfo<Member> pageInfo = new PageInfo<>(memberList);
        model.addAttribute("pageInfo",pageInfo);
        return "member-list";
    }

    @RequestMapping("/save")
    public String save(Member member){
        memberService.InsertMember(member);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteMember")
    public String deleteMember(Integer id){
        memberService.deleteMember(id);
        return "redirect:findAll";
    }

}
